<?php

namespace App\Http\Controllers\Api;

use App\Models\Member;
use App\Models\MemberLabel;
use App\Models\Order;
use App\Models\OrderProduct;
use App\Models\ParkDeliverTime;
use App\Models\Printer;
use App\Models\Supplier;
use App\Repositories\AddressRepository;
use App\Repositories\Api\MemberRepository;
use App\Repositories\OrderRepository;

class TestController extends MiniAppController
{
    private $memberRep = NULL;
    public const SECRET_KEY = '@4!@#$%@';

    public function __construct(
        MemberRepository $memberRep
    )
    {
        parent::__construct();
        $this->memberRep = $memberRep;
    }

    /**
     * thomashtao
     */
    public function tao()
    {

    }

    public function test()
    {
        /*$start_date = "2024-09-20";
       $end_date = "2024-09-30";*/
        $start_date = request()->get("sdate");
        $end_date = request()->get("edate");
        if (empty($start_date)) {
            echo "请输入开始日期";
            exit();
        }
        if (empty($end_date)) {
            echo "请输入结束日期";
            exit();
        }

        echo "统计数据： " . $start_date . '~' . $end_date . "之间<br/><br/><br/>";
        $this->stat($start_date, $end_date);
        die();


        $arr = [
            "7:30-8:00",
            "8:00-8:30",
            "8:30-9:00",
            "10:30-11:00",
            "11:00-11:30",
            "11:30-12:00",
            "12:00-12:30",
            "12:30-13:00",
            "13:00-13:30",
            "13:30-14:00",
            "16:30-17:00",
            "17:30-18:00",
            "18:00-18:30",
            "18:30-19:00",
            "19:00-19:30",
            "19:30-20:00",
        ];

        $data = [];
        foreach ($arr as $v) {
            $tmp = explode("-", $v);
            $data[] = [
                "start_time" => $tmp[0],
                "end_time" => $tmp[1],
                'sort' => 60,
                'status' => 1,
                'create_time' => date("Y-m-d H:i:s"),
                'update_time' => date("Y-m-d H:i:s"),
            ];
        }

        ParkDeliverTime::insert($data);
        dd();

        /*echo 333;
        die();
        (new SendSmsService())->send("13814547582", [
            'name' => "taos",
            'store' => "测试店铺"
        ]);*/
        $order_id = 111104;
        $shopId = 31;
        $printerId = Printer::query()->where(["shop_id" => $shopId, 'type' => 1, 'status' => 1])->orderBy("id", "desc")->value("id");

        dd($printerId);
        //        $this->printerOrder(['orderId' => $order_id], $printerId, 2);

        die();
        $order = new OrderRepository();

        $order->updateOrder(date("Ymdhis"), "240121225035568309");

        die();
        $list = (new OrderRepository())->autoCancelOrder();
        dd();
        $res = (new AddressRepository())->getAreas();

        die();
        $order = new OrderRepository();

        $order->updateOrder(date("Ymdhis"), "240121225035568309");
    }

    public function stat($start_date = null, $end_date = null)
    {
//        $date = date("Y-m-06");

        /*$start_date = "2024-09-20";
        $end_date = "2024-09-30";*/

        $start_date_1 = strtotime($start_date);
        $end_date_1 = strtotime($end_date);

        //会员统计
        $memberIds = Member::query()
            ->whereBetween("reg_time", [$start_date_1, $end_date_1])
//                ->where("reg_time", ">=", strtotime($start_date))
            ->pluck("id")->toArray();
        $total_member_amount = Member::query()->whereRaw("1=1")->count("id");

        //医生标签
        $doctor_amount = MemberLabel::query()->where("type", "=", 2)->count("id");

        //总金额
        $total_price = Order::query()
//            ->where("create_time", ">=", strtotime($date))
            ->whereBetween("create_time", [$start_date_1, $end_date_1])
            ->whereIn("status", [10, 20])
            ->sum("total_price");

        $total_amount = Order::query()
//            ->where("create_time", ">=", strtotime($date))
            ->whereBetween("create_time", [$start_date_1, $end_date_1])
            ->whereIn("status", [10, 20])
            ->count("id");

        //付费会员
        $pay_member_amount = Order::query()
            ->distinct("uid")
            ->whereIn("status", [10, 20, 60])
            ->count("uid");

        //v2 付费会员
        $new_pay_member_amount = Order::query()
//            ->where("create_time", ">=", strtotime($date))
            ->whereBetween("create_time", [$start_date_1, $end_date_1])
            ->distinct("uid")
            ->whereIn("status", [10, 20, 60])
            ->count("uid");

        //食堂金额
        $total_price_1 = Order::query()
//            ->where("create_time", ">=", strtotime($date))
            ->whereBetween("create_time", [$start_date_1, $end_date_1])
            ->whereIn("status", [10, 20])
            ->where(['shop_type' => 1])
            ->sum("total_price");

        //食堂总订单数
        $total_amount_1 = Order::query()
//            ->where("create_time", ">=", strtotime($date))
            ->whereBetween("create_time", [$start_date_1, $end_date_1])
            ->whereIn("status", [10, 20])
            ->where(['shop_type' => 1])
            ->count("id");

        $order_groups = Order::query()
//            ->where("create_time", ">=", strtotime($date))
            ->whereBetween("create_time", [$start_date_1, $end_date_1])
            ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(create_time), '%Y-%m-%d') AS cdate,sum(total_price) as total_price")
            ->whereIn("status", [10, 20])
            ->groupBy("cdate")
            ->orderBy("total_price", "desc")
            ->get()
            ->toArray();

        echo "总收入：" . $total_price . "<br/>" .
            "总订单数：" . $total_amount . "<br/>" .
            "新增会员数：" . count($memberIds) . "<br/>" .
            "总会员：" . $total_member_amount . "<br/>" .
            "付费总会员：" . $pay_member_amount . "<br/>" .
            "V2付费会员：" . $new_pay_member_amount . "<br/>" .
            "医生标签会员数：" . $doctor_amount . "<br/>" .
            "其他标签会员数：" . ($total_member_amount - $doctor_amount) . "<br/>" .
            "食堂总收入：" . $total_price_1 . "<br/>" .
            "食堂总订单数：" . $total_amount_1 . "<br/>" .
            "其他单店总收入：" . ($total_price - $total_price_1) . "<br/>" .
            "单店总订单数：" . ($total_amount - $total_amount_1) . "<br/>" .
            "单日最高总金额：" . $order_groups[0]['total_price'] . "<br/>" .
            "单日最低总金额：" . $order_groups[count($order_groups) - 1]['total_price'] . "<br/><br/><br/>";

        $suppliers = Supplier::query()->where(['type' => 0, 'status' => 1])->get(['id', 'title'])->toArray();

        $order_products = OrderProduct::query()
            ->selectRaw("product_title,sum(amount) as amount")
//            ->where("create_time", ">", $date)
            ->whereBetween("create_time", [$start_date, $end_date])
            ->groupBy("product_id")
            ->orderBy("amount", "desc")
            ->limit(10)
            ->get()
            ->toArray();

        $str = "";
        foreach ($order_products as $k => $v) {
            $str .= ($k + 1) . ". " . $v['product_title'] . "：" . $v['amount'] . "件<br/>";
        }
        echo "<br />" . $str . "<br />";

        //单店统计
        foreach ($suppliers as $k => $supplier) {

            $shop_total_price = Order::query()
//                ->where("create_time", ">=", strtotime($date))
                ->whereBetween("create_time", [$start_date_1, $end_date_1])
                ->where(['shop_type' => 0, 'shop_id' => $supplier['id']])
                ->whereIn("status", [10, 20])
                ->sum("total_price");

            $shop_total_amount = Order::query()
//                ->where("create_time", ">=", strtotime($date))
                ->whereBetween("create_time", [$start_date_1, $end_date_1])
                ->where(['shop_type' => 0, 'shop_id' => $supplier['id']])
                ->whereIn("status", [10, 20])
                ->count("id");

            //医生标签订单数
            $doctor_uids = MemberLabel::query()->where(['type' => 2])->pluck("uid")->toArray();
            $doctor_total_amount = Order::query()
//                ->where("create_time", ">=", strtotime($date))
                ->whereBetween("create_time", [$start_date_1, $end_date_1])
                ->where(['shop_type' => 0, 'shop_id' => $supplier['id']])
                ->whereIn("uid", $doctor_uids)
                ->whereIn("status", [10, 20])
                ->count("id");


            $shop_order_groups = Order::query()
//                ->where("create_time", ">=", strtotime($date))
                ->whereBetween("create_time", [$start_date_1, $end_date_1])
                ->where(['shop_type' => 0, 'shop_id' => $supplier['id']])
                ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(create_time), '%Y-%m-%d') AS cdate,sum(total_price) as total_price")
                ->whereIn("status", [10, 20])
                ->groupBy("cdate")
                ->orderBy("total_price", "desc")
                ->get()
                ->toArray();

            $shop_max_price = count($shop_order_groups) > 0 ? $shop_order_groups[0]['total_price'] : 0;
            $shop_min_price = count($shop_order_groups) > 0 ? $shop_order_groups[count($shop_order_groups) - 1]['total_price'] : 0;

            $shop_order_groups_amount = Order::query()
//                ->where("create_time", ">=", strtotime($date))
                ->whereBetween("create_time", [$start_date_1, $end_date_1])
                ->where(['shop_type' => 0, 'shop_id' => $supplier['id']])
                ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(create_time), '%Y-%m-%d') AS cdate,count(id) as amount")
                ->whereIn("status", [10, 20])
                ->groupBy("cdate")
                ->orderBy("amount", "desc")
                ->get()
                ->toArray();

            $shop_max_amount = count($shop_order_groups_amount) > 0 ? $shop_order_groups_amount[0]['amount'] : 0;
            $shop_min_amount = count($shop_order_groups_amount) > 0 ? $shop_order_groups_amount[count($shop_order_groups_amount) - 1]['amount'] : 0;

            echo "店铺名称：" . $supplier['title'] . "<br/>" .
                "总收入：" . $shop_total_price . "<br/>" .
                "总订单数：" . $shop_total_amount . "<br/>" .
                "医生标签订单数：" . $doctor_total_amount . "<br/>" .
                "其他标签订单数：" . ($shop_total_amount - $doctor_total_amount) . "<br/>" .
                "单日最高总金额：" . $shop_max_price . "<br/>" .
                "单日最低总金额：" . $shop_min_price . "<br/>" .
                "单日最高订单数：" . $shop_max_amount . "<br/>" .
                "单日最低订单数：" . $shop_min_amount . "<br/>" .
                "<br/><br/><br/>";
        }


    }

}
